package huoye.shops.base;


import android.util.Log;

import com.codeideology.android.http.okhttp.OkHttpUtils;
import com.codeideology.android.http.okhttp.builder.PostFormBuilder;
import com.codeideology.android.http.okhttp.callback.StringCallback;
import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.log.LogUtils;
import com.codeideology.android.rpc.PojoJsonResponseHandler;
import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import huoye.shops.common.model.CodeData;
import huoye.shops.common.model.OssImageData;
import huoye.shops.huoye.user.model.UpdateInfo;
import huoye.shops.utils.MapFile;
import huoye.shops.utils.StockUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by user on 2017/7/25.
 */

public class BaseService {

 /*   protected String getUrl(String uri) {
        return APIUrls.HTTP + APIUrls.API_HOST + uri;
    }*/
   protected String getUrl(String uri) {
        return UserManager.getInstance().getUrl() + uri;
    }

    protected void setUserId(Map<String, String> param) {
        param.put("userId", UserManager.getInstance().getUserId());
    }



    protected void setUserShopId(Map<String, String> param) {
        param.put("shopId", UserManager.getInstance().getUserShopId());
    }

    protected void setUser(Map<String, String> param) {
        param.put("userId", UserManager.getInstance().getUserId());
        param.put("shopId", UserManager.getInstance().getUserShopId());
    }

    protected void setUserShopList(Map<String, String> param, Paging paging) {
        param.put("userId", UserManager.getInstance().getUserId());
        param.put("shopId", UserManager.getInstance().getUserShopId());
        param.put("pageIndex", paging.getCurrentPage() + "");
        param.put("pageSize", paging.getPageSize() + "");
    }


    protected void setPaging(Map<String, String> param, Paging paging) {
        param.put("pageIndex", paging.getCurrentPage() + "");
        param.put("pageSize", paging.getPageSize() + "");
    }

    protected void setPagingSize(Map<String, String> param, Paging paging) {
        param.put("page_size", paging.getPageSize() + "");
    }


    /**
     * 上传单张OSS图片
     *
     * @param id
     * @param callback
     */
    public void uploadOssImg(String id, String type, String filePath, MineBaseJsonRequestCallback<OssImageData> callback) {

        File file = new File(filePath);
        OkHttpUtils.post().url(getUrl(APIUrls.COMMON_CONFIG.TYPE_COMMON_UPLOAD_IMG))
                .addParams("id", id)
                .addParams("type", type)
                .addParams("filePath", "product")
                .addFile("FileName", file.getName(), file)//第一参数填写key，第二个是图 片名字，第三个是流
                .build()
                .execute(callback);
    }

    /**
     * 上传多张OSS图片
     *
     * @param id
     * @param type
     * @param imgUrl
     * @param callback
     */
    public void uploadOssImgMore(String id, String type, List<String> imgUrl, MineBaseJsonRequestCallback<OssImageData> callback) {

        PostFormBuilder builder = OkHttpUtils.post();
        builder.url(getUrl(APIUrls.COMMON_CONFIG.TYPE_COMMON_UPLOAD_IMG));

        for (int i = 0; i < imgUrl.size(); i++) {
            File file = new File(imgUrl.get(i));
            builder.addFile("FileName", file.getName(), file);//第一参数填写key，第二个是图片名字，第三个是流

        }
        builder.addParams("id", id)
                .addParams("type", type)
                .addParams("filePath", "product")
                .build()
                .execute(callback);
    }

    /**
     * 删除OSS图片
     *
     * @param filePath
     * @param callback
     */
    public void deleteOssImg(String filePath, MineBaseJsonRequestCallback<OssImageData> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("imgUrl", filePath);
        OkHttpUtils.get().url(getUrl(APIUrls.COMMON_CONFIG.TYPE_COMMON_DELETE_IMG))
                .params(param)
                .build()
                .execute(callback);

    }


    /**
     * 生成活动扫描二维码URL    etClassCode/活动类型  eventId/活动ID
     *
     * @param callback
     */
    public void getCodeUrl(String eventId, MineBaseJsonRequestCallback<CodeData> callback) {

        Map<String, String> param = new HashMap<>();

        param.put("eventId", eventId);
        setUserShopId(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.COMMON_CONFIG.TYPE_COMMON_FIND_EVENTS_TO_URL))
                .params(param)
                .build().execute(callback);
    }


    //检测升级
    public void checkUpdate( MineBaseJsonRequestCallback<UpdateInfo> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("apptype","1");

        OkHttpUtils.getInstance().get()
                .url(APIUrls.COMMON_CONFIG.TYPE_COMMON_APP_VERSION)
                .params(param)
                .build().execute(callback);
    }

}
